// pages/order/order-detail.js
const format = require('../../utils/format.js')
const app = getApp()
let timer = null

function getActionContent(orderStatus) {
    const status = parseInt(orderStatus)
    let r = ''
    switch (status) {
        case 0: //待付款
            r = "好不容易选好了,再想想呗~"
            break
        case 20: //待配送
            r = "马上就发货啦,真的不要了吗~"
            break
    }
    return r
}
Page({

    data: {
        remain_time: '',
        showQrCode: false,
        showReason: false
    },

    onLoad: function(options) {
        options.cid = wx.getStorageSync('cid')
        this.setData(options)
        let tabs = app.tabs
        let statusObj = {}
        Object.keys(tabs).forEach(k => {
            statusObj[tabs[k].status] = tabs[k].name
        })
        Object.assign(statusObj, {
            '70': '待退款',
            '71': '退款完成',
        })
        this.setData({
            statusObj,
            tabs
        })
        // app.loadCode()
        // this.loadGroup()
        this.loadData()
    },
    onHide() {
        clearInterval(timer);
    },
    onUnload() {
        clearInterval(timer);
    },
    isQrcode() {
        this.setData({
            showQrCode: !this.data.showQrCode
        })
    },
    checkRefoundPop() {
        this.setData({
            showReason: !this.data.showReason
        })
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.get('order/detail', {
            data: {
                order_id: this.data.order_id
            }
        }).then(res => {
            let t = new Date(parseInt(res.data.created_at) * 1000);
            res.data.created_time = format.dateformat.format(t)
            if (res.data.status == 0) {
                let start_time = Math.round(new Date() / 1000)
                let times = 30 * 60 - (start_time - res.data.created_at)
                if (times > 0) {
                    this.countDown(times)
                }
            }
            res.data.takeCode = res.data.take_code.split('')
            this.setData(res.data)
            wx.hideLoading()
        })
    },

    // loadGroup() {
    //     let order_type = this.data.order_type
    //     let url = order_type == 1 ? 'location/info-admin' : 'supermarket/info'
    //     app.http.get(url, {
    //         data: {
    //             cid: this.data.cid
    //         }
    //     }).then(res => {
    //         this.setData({
    //             group_info: order_type == 1 ? res.data.data : res.data
    //         })
    //     })
    // },
    action() {
        let status = this.data.status
        wx.showModal({
            title: '温馨提示',
            content: getActionContent(status),
            confirmText: '残忍拒绝',
            cancelText: '再想想',
            confirmColor: '#ff5400',
            success: res => {
                if (res.confirm) {
                    if (status == 0) {
                        this.cancel()
                        return
                    }
                    if (status == 20) {
                        this.payBack()
                    }
                }
            }
        })

    },
    //继续支付
    toPay() {
        if (this.data.paying) {
            return
        }
        this.setData({
            paying: true
        })
        wx.showLoading({
            title: '请稍等',
        })
        app.http.post('order/pay', {
            data: {
                order_id: this.data.order_id
            }
        }).then(res => {
            wx.hideLoading()
            this.setData({
                paying: false
            })
            if (res.data.status != 1) {
                wx.showModal({
                    title: '支付失败',
                    content: res.data.msg,
                    showCancel: false,
                    confirmColor: '#ff5400'
                })
                return
            }
            let jsPay = res.data.js
            let orderInfo = res.data.info
            if (orderInfo.pay_type == 2) { //余额支付
                this.setData({
                    status: orderInfo.order_status
                })
                wx.showToast({
                    icon: 'none',
                    title: '支付成功',
                })
                return
            }
            const obj = Object.assign(jsPay, { //微信支付
                success: res => {
                    this.setData({
                        status: orderInfo.order_status
                    })
                    wx.showToast({
                        icon: 'none',
                        title: '支付成功',
                    })
                },
                fail: err => {
                    wx.showToast({
                        icon: 'none',
                        title: '支付已取消',
                    })
                }
            });
            wx.requestPayment(obj)

        }).catch(err => {
            this.setData({
                paying: false
            })
        })
    },
    //取消订单
    cancel() {
        wx.showLoading({
            title: '取消中',
        })
        app.http.post('order/cancel', {
            data: {
                order_id: this.data.order_id,
            }
        }).then(res => {
            wx.hideLoading()
            if (res.data.status == 1) {
                this.setData({
                    status: res.data.order_status,
                    remain_time: 0
                })
                wx.showToast({
                    icon: 'none',
                    title: '取消订单成功',
                })
            }
        })
    },
    //退款
    refound() {
        wx.showLoading({
            title: '请稍等',
        })
        app.http.post('order/return-of-goods', {
            data: {
                order_id: this.data.order_id,
                return_reason: this.data.reason
            }
        }).then(res => {
            wx.hideLoading()
            this.checkRefoundPop()
            wx.showModal({
                title: '温馨提示',
                content: '你的申请已进入审核阶段,请耐心等待',
                confirmColor: '#ff5400',
                showCancel: false,
                success: res => {
                    if (res.confirm) {
                        this.loadData()
                    }
                }
            })
        })
    },
    //取消退款
    cancelRefound() {
        wx.showModal({
            title: '温馨提示',
            content: '您确定要撤回正在审核的退款申请吗',
            confirmColor: '#ff5400',
            success: res => {
                if (res.confirm) {
					app.http.post('order/cancel-refunds', {
                        data: {
                            order_id: this.data.order_id,
                        }
                    }).then(res => {
                        this.loadData()
                    })
                }
            }
        })
    },

    //查看大图二维码
    showBigQrCode() {
        let arr = [this.data.code_url]
        wx.previewImage({
            urls: arr,
        })
    },
    /**
     * 倒计时
     */
    countDown: function(times) {
        timer = setInterval(() => {
            var day = 0,
                hour = 0,
                minute = 0,
                second = 0;
            day = Math.floor(times / (60 * 60 * 24));
            hour = Math.floor(times / (60 * 60)) - (day * 24);
            minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);
            second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);

            minute = minute <= 9 ? '0' + minute : minute
            second = second <= 9 ? '0' + second : second

            this.setData({
                remain_time: minute + ':' + second,
            })
            times--;
            if (times <= 0) {
                clearInterval(timer);
                this.setData({ //倒计时结束  已取消
                    status: -1
                })
            }
        }, 1000);
    },

    selectedCodePop: function() {
        this.setData({
            isCodePop: !this.data.isCodePop
        })
    },
    bindinput(evt) {
        this.setData({
            reason: evt.detail.value
        })
    }
})